
const path = require("path")
const sql = require("../module")
const md5 = require("md5")
const register = {
}
register.views = (req, res) => {
    res.sendFile(path.resolve(__dirname, "../views/register.html"))
}

register.register = (req, res) => {
    console.log(req.body);
    let { u, p } = req.body
    let str = `SELECT * from userinfo where username = "${u}"`
    sql.query(str).then(([data]) => {
        //                                       单纯结构数组下标0的数据
        if (data.length == 0) {
            //  插入一新数据
            let str = `insert into userinfo(username,password) values('${u}','${md5(p)}')`
            sql.query(str).then(() => {
                res.json({
                    code: 2000,
                    message: "注册成功"
                })
            })


        } else {
            res.json({
                code: 4003,
                message: "用户名已被占用"
            })
        }
    })
    //  查询数据库 有没有该账号 没有就 插入一个新的数据
}



module.exports = register